package com.hjq.www.View;
import com.hjq.www.Dao.AnimalDao;
import com.hjq.www.Dao.Impl.AnimalDaoImpl;
import com.hjq.www.utils.JDBCUtils;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Vector;

/**
 * Create with IntelliJ IDEA
 *
 * @author: hjq
 * @Date:
 * @Description:
 */
public class AnimalView extends JFrame {
    public static void main(String[] args) {
        new AnimalView().AnimalMaster();
    }
    Connection conn = null;
    AnimalDao a=new AnimalDaoImpl();
    JLabel idLabel = new JLabel("序    号: ");
    JTextField idField = new JTextField(15);
    JLabel nameLabel = new JLabel("名    称: ");
    JTextField nameField = new JTextField(15);
    JLabel typeLabel = new JLabel("种    类: ");
    JTextField typeField = new JTextField(15);
    JLabel introductionLabel = new JLabel("简    介: ");
    JTextField introductionField = new JTextField(15);
    JLabel ageLabel = new JLabel("年    龄: ");
    JTextField ageField = new JTextField(15);
    JLabel statusLabel = new JLabel("健康状况: ");
    JTextField statusField = new JTextField(15);
    JLabel zidLabel = new JLabel("园 区 id: ");
    JTextField zidField = new JTextField(15);
        public AnimalView(){
            JFrame frame = new JFrame();
            DefaultTableModel dtm = new DefaultTableModel();
            DefaultTableCellRenderer dtr = new DefaultTableCellRenderer();
            dtr.setHorizontalAlignment(JLabel.CENTER);
            JTable table = new JTable(stuTableModel(dtm));
            table.setDefaultRenderer(Object.class, dtr);
            JScrollPane jsp = new JScrollPane();
            jsp.setViewportView(table);
            frame.add(jsp);
            frame.setBounds(1, 10, 1500, 170);
            JPanel panel = new JPanel();
            frame.add(panel, BorderLayout.NORTH);
            JButton button1 = null;
            JPanel Jpanel = new JPanel();
            JButton resetButton = new JButton("刷新");
            Jpanel.add(resetButton);
            JButton backButton = new JButton("返回");
            backButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    new MainView().init();
                    dispose();
                }
            });
            Jpanel.add(backButton);
            resetButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    new AnimalView();
                }
            });
            frame.setTitle("园区动物信息表");
            frame.add(Jpanel, BorderLayout.SOUTH);
            frame.setVisible(true);
        }
        public void AnimalMaster() {
            Container container = getContentPane();
            setBounds(520, 180, 500, 650);
            JLabel idLabel = new JLabel("序    号: ");
            JTextField idField = new JTextField(15);
            JLabel nameLabel = new JLabel("名    称: ");
            JTextField nameField = new JTextField(15);
            JLabel typeLabel = new JLabel("种    类: ");
            JTextField typeField = new JTextField(15);
            JLabel introductionLabel = new JLabel("简    介: ");
            JTextField introductionField = new JTextField(15);
            JLabel ageLabel = new JLabel("年    龄: ");
            JTextField ageField = new JTextField(15);
            JLabel statusLabel = new JLabel("健康状况: ");
            JTextField statusField = new JTextField(15);
            JLabel zidLabel = new JLabel("园 区 id: ");
            JTextField zidField = new JTextField(15);
            JButton addButton = new JButton("添加");
            JButton updateButton = new JButton("修改");
            JButton searchButton = new JButton("查询");
            JButton deleteButton = new JButton("删除");
            JButton resetButton = new JButton("重置");
            //添加一块桌布
            container.setLayout(new BorderLayout());
            JPanel root = new JPanel();
            root.setLayout(null);
            Font f = new Font("宋体", Font.PLAIN, 25);
            idLabel.setBounds(17, 5, 130, 60);
            idField.setBounds(145, 20, 300, 35);
            nameLabel.setBounds(17, 65, 130, 60);
            nameField.setBounds(145, 80, 300, 35);
            typeLabel.setBounds(17, 125, 130, 60);
            typeField.setBounds(145, 140, 300, 35);
            ageLabel.setBounds(17, 185, 130, 60);
            ageField.setBounds(145, 200, 300, 35);
            introductionLabel.setBounds(17, 245, 130, 60);
            introductionField.setBounds(145, 260, 300, 35);
            statusLabel.setBounds(17, 305, 130, 60);
            statusField.setBounds(145, 320, 300, 35);
            zidLabel.setBounds(17, 365, 130, 60);
            zidField.setBounds(145, 380, 300, 35);
            idLabel.setFont(f);
            nameLabel.setFont(f);
            typeLabel.setFont(f);
            introductionLabel.setFont(f);
            ageLabel.setFont(f);
            statusLabel.setFont(f);
            zidLabel.setFont(f);
            root.add(idLabel);
            root.add(idField);
            root.add(nameLabel);
            root.add(nameField);
            root.add(typeLabel);
            root.add(typeField);
            root.add(ageLabel);
            root.add(ageField);
            root.add(introductionLabel);
            root.add(introductionField);
            root.add(statusLabel);
            root.add(statusField);
            root.add(zidLabel);
            root.add(zidField);
            addButton.setBounds(30, 450, 80, 50);
            updateButton.setBounds(140, 450, 80, 50);
            searchButton.setBounds(250, 450, 80, 50);
            deleteButton.setBounds(360, 450, 80, 50);
            resetButton.setBounds(200,540,80,50);
            setTitle("动物信息操作");
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            addButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    a.add(idField, nameField, typeField, ageField, introductionField, statusField, zidField);
                }
            });
            root.add(addButton);
            updateButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    a.delete(idField);
                    a.add(idField,nameField, typeField, ageField, introductionField, statusField,zidField);
                }
            });
            root.add(updateButton);
            searchButton.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                    a.search(idField, nameField, typeField, ageField, introductionField, statusField, zidField);
                }
            });
            root.add(searchButton);
            deleteButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    a.delete(idField);
                }
            });
            root.add(deleteButton);
            resetButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    a.reset();
                }
            });
            root.add(resetButton);
            container.add(root);
            this.setVisible(true);
        }

    /**
     * 使用DefaultTableModel导入表中数据
     *
     * @param dtm
     * @return
     */
    private DefaultTableModel stuTableModel(DefaultTableModel dtm) {
        Statement st = null;
        ResultSet rs = null;
        String[] stu = {"序号", "名称", "种类", "年龄(岁)", "简介", "健康状态", "园区id"};
        dtm.setColumnIdentifiers(stu);
        Vector v = null;
        try {
            conn = JDBCUtils.getConnection();
            st = conn.createStatement();
            String sql = "select * from animals";
            rs = st.executeQuery(sql);
            while (rs.next()) {
                v=new Vector();
                String sno = rs.getString("id");
                String name = rs.getString("name");
                String type = rs.getString("type");
                String age = rs.getString("age");
                String introduction = rs.getString("introduction");
                String status = rs.getString("status");
                String zid = rs.getString("zid");
                v.add(sno);
                v.add(name);
                v.add(type);
                v.add(age);
                v.add(introduction);
                v.add(status);
                v.add(zid);
                dtm.addRow(v);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return dtm;
    }
}
